<?php

/* New User page
 *
 * Adds a username and password hash to the database
 */

?>



<html>
<head>
    <title>Enter New User</title>
</head>

<script language="Javascript" type="text/javascript" src="./crypto/sha1.js"></script>
<script language="Javascript" type="text/javascript" src="./crypto/blowfish.js"></script>
<script language="JavaScript" type="text/javascript" src="./crypto/jsbn.js"></script>
<script language="JavaScript" type="text/javascript" src="./crypto/jsbn2.js"></script>
<script language="JavaScript" type="text/javascript" src="./crypto/prng4.js"></script>
<script language="JavaScript" type="text/javascript" src="./crypto/rng.js"></script>

<script language="JavaScript" type="text/javascript" src="./crypto/rsa.js"></script>
<script language="JavaScript" type="text/javascript" src="./crypto/rsa2.js"></script>
<script language="JavaScript">


function do_genrsa() {
  var rsa = new RSAKey();
  var dr = document.rsatest;
  rsa.generate(parseInt(dr.bits.value),dr.pk_e.value);
  dr.pk_n.value = linebrk(rsa.n.toString(16),64);
  dr.sk_d.value = linebrk(rsa.d.toString(16),64);
  dr.sk_p.value = linebrk(rsa.p.toString(16),64);
  dr.sk_q.value = linebrk(rsa.q.toString(16),64);
  dr.sk_dmp1.value = linebrk(rsa.dmp1.toString(16),64);
  dr.sk_dmq1.value = linebrk(rsa.dmq1.toString(16),64);
  dr.sk_coeff.value = linebrk(rsa.coeff.toString(16),64);
}

</script>

<body onLoad='do_genrsa();'>

<h1>New User Creation</h1>

<p>Please enter desired username and password.</p>

<form method="post" name="rsatest" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="hidden" name="bits" value="512">

<br />Username: <input type="text" name="username" value="">

<br />Password: <input type="password" name="password" value="">

<br />Confirm Password: <input type="password" name="passwordconfirm" value="">

<br />Public Key e: <input type="text" name="pk_e" value="3">
	
<br />Public Key n: <textarea cols="80" rows="4" name="pk_n"></textarea>

<br />Private Key d: <textarea cols="80" rows="4" name="sk_d"></textarea>

<br />Private Key p: <textarea cols="80" rows="4" name="sk_p"></textarea>

<br />Private Key q: <textarea cols="80" rows="4" name="sk_q"></textarea>

<br />Private Key dmp1: <textarea cols="80" rows="4" name="sk_dmp1"></textarea>

<br />Private Key dmq1: <textarea cols="80" rows="4" name="sk_dmq1"></textarea>

<br />Private Key coeff: <textarea cols="80" rows="4" name="sk_coeff"></textarea>

	
<input type="Submit" name="action" value="Submit">

<?

// Post information to this page and process

if ($_POST['action'] == "Submit") {
    extract ($_POST);
	
	$sk_e = $pk_e;
	$sk_n = $pk_n;
	
	/* Hash the password */
	$password = MD5($password);
    $passwordconfirm = MD5($passwordconfirm);
	
	
    if ($username == "" || $password == "" || $passwordconfirm == "") {
        echo "<p><span style=\"color:red;\">Please enter all requested information.</span></p>";
    }
    else if ($password != $passwordconfirm) {
        echo "<p><span style=\"color:red;\">Your password did not match, please try again.</span></p>";
    }
	/* 
	else if (checkusername($username)) {
        echo "<p><span style=\"color:red;\">That username is already taken, please select another.</span></p>";
    }
	*/
	else {
	
		// Process it
		require("./dbconnect.inc.php");

		mysql_connect($host,$dbuser,$dbpassword);
		@mysql_select_db($database) or die( "Unable to select database");
		
		$pk_e = str_replace("\n", "", $pk_e);
		$pk_n = str_replace("\n", "", $pk_n);
		$sk_e = str_replace("\n", "", $sk_e);
		$sk_n = str_replace("\n", "", $sk_n);
		$sk_d = str_replace("\n", "", $sk_d);
		$sk_p = str_replace("\n", "", $sk_p);
		$sk_q = str_replace("\n", "", $sk_q);
		$sk_dmp1 = str_replace("\n", "", $sk_dmp1);
		$sk_dmq1 = str_replace("\n", "", $sk_dmq1);
		$sk_coeff = str_replace("\n", "", $sk_coeff);
		

		// Enter data
		$query = sprintf("INSERT INTO users VALUES ('','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s')",
		$username, $password, $pk_e, $pk_n, $sk_e, $sk_n, $sk_d, $sk_p, $sk_q, $sk_dmp1, $sk_dmq1, $sk_coeff);

		mysql_query($query);

		mysql_close();
		
        echo "<p><span style=\"color:blue;\">User added.</span></p>";
    }
}

?>

</form>
</body>
</html>